package runner;

import net.base.annotations.NotNull;
import net.socket.utils.LogUtil;

import java.io.BufferedReader;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/3/23 下午4:51
 * @UpdateDate:
 * @Description:
 */
public class ServerReporter implements Runnable {

    private BufferedReader reader;
    private String clientName;

    ServerReporter(@NotNull BufferedReader reader, String clientName){
        this.reader = reader;
        this.clientName = clientName;
    }

    @Override
    public void run() {
        String line = null;
        while (reader != null){
            try {
                line = reader.readLine();
                if(line != null){
                    System.out.println(line);
                }
            }catch (Throwable e){
                e.printStackTrace();
                setReader(null);
                break;
            }
        }
    }

    public void setReader(BufferedReader reader){
        if(reader == null){
            LogUtil.info(clientName + "'s reporter ending!");
        }
        this.reader = reader;
    }

    public BufferedReader getReader() {
        return reader;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }
}
